package com.atguigu.edusugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.edusugar.bean.exam;
import com.atguigu.edusugar.service.TrafficService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class sugarController {
    @Autowired
    TrafficService trafficService;
    @ResponseBody
    @RequestMapping("sugar/traffic")
    public String traffic(Integer date){
        if (date == null) {
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }

        List<exam> exam1 = trafficService.exam(date);
        System.out.println(exam1);
        JSONObject page = new JSONObject();
        page.put("status",0);
        page.put("msg","");
        JSONObject data1 = new JSONObject();

        HashSet<Object> categories = new HashSet<>();
        for (exam ex : exam1) {
            categories.add(ex.getPaper_id());
        }
        data1.put("categories",categories);


        HashSet series =new HashSet<>();
        JSONObject data3 = new JSONObject();
        data3.put("name","平均分");
        HashSet<Object> data4 = new HashSet<>();
        for (exam ex : exam1) {
            data4.add(ex.getAvg_score());
        }
        data3.put("data",data4);
        series.add(data3);
        data1.put("series",series);
        page.put("data",data1);

        return page.toJSONString();

    }
}
